<?php
namespace api\modules\v1\controllers;

use api\modules\v1\models\user\UserAccount;
use cms\modules\v1\models\Captcha;
use common\models\Functions;
use Yii;
use yii\filters\auth\HttpHeaderAuth;
use yii\helpers\ArrayHelper;

/**
 * Default controller for the `v1` module
 */
class AccountController extends DefaultController
{

    public $modelClass = '';
    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            'authenticator' => [
                'class' =>  HttpHeaderAuth::className(),    //head头部验证
                'header' => 'access-token',
                'optional' => [                     //需要过滤掉不用token参数的方法
                    'register', 'in-pass', 'in-code','forget','captcha'
                ]
            ]
        ]);
    }

    //注册图形验证码
    public function actionCaptcha() {
        if (Yii::$app->request->isGet) {
            if (Yii::$app->request->isGet) {
                return (new Captcha())->create();
            }
        }
    }

    //注册
    public function actionRegister() {
        if (Yii::$app->request->isPost) {
            $model = new UserAccount();
            $model->setScenario('register');
            if ($model->load(Yii::$app->request->post(), '')) {
                return $model->userRegister();
            }
        }
    }

    //账号密码登录
    public function actionInPass() {
        if (Yii::$app->request->isPost) {
            $model = new UserAccount();
            $model->setScenario('loginByPass');
            if ($model->load(Yii::$app->request->post(), '')) {
                return $model->doLoginByPass();
            }
        }
    }

    //验证码登录
    public function actionInCode() {
        if (Yii::$app->request->isPost) {
            $model = new UserAccount();
            $model->setScenario('loginByCode');
            if ($model->load(Yii::$app->request->post(), '')) {
                return $model->doLoginByCode();
            }
        }
    }


    // 忘记密码
    public function actionForget() {
        if (Yii::$app->request->isPost) {
            $model = new UserAccount();
            $model->setScenario('forgetPassword');
            if ($model->load(Yii::$app->request->post(), '')) {
                return $model->forgetPasswordForLogin();
            }
        }
    }

    // 修改密码
    public function actionModifyPassword() {
        if (Yii::$app->request->isPost) {
            $model = new UserAccount();
            $model->setScenario('modifyPassword');
            if ($model->load(Yii::$app->request->post(), '')) {
                return $model->modifyPassword();
            }
        }
    }

    // 设置修改支付密码
    public function actionPayPassword() {
        if (Yii::$app->request->isPost) {
            $model = new UserAccount();
            $model->setScenario('payPassword');
            if ($model->load(Yii::$app->request->post(), '')) {
                return $model->modifyPayPassword();
            }
        }
    }



    /**
     * 退出
     * @return array
     */
    public function actionOut() {
        if (\Yii::$app->request->isGet) {
            return (new UserAccount())->logout();
        }
    }
}
